package com.mj.app.health.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mj.app.health.MyApplication;
import com.mj.app.health.R;
import com.mj.app.health.base.BaseActivity;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.LoginResponse;
import com.mj.app.health.util.SharedPreferanceUtil;
import com.mj.app.health.util.ToastUtil;

import butterknife.BindView;

/**
 * ========================================
 * *
 * title: LoginActivity
 * description:
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.com_back)
    ImageView comBack;
    @BindView(R.id.com_btn)
    TextView comBtn;
    @BindView(R.id.com_title)
    TextView comTitle;
    @BindView(R.id.com_title_rl)
    RelativeLayout comTitleRl;
    @BindView(R.id.login_username)
    EditText loginUsername;
    @BindView(R.id.login_userpwd)
    EditText loginUserpwd;
    @BindView(R.id.login_commit_btn)
    Button loginCommitBtn;
    @BindView(R.id.login_change_regist)
    TextView loginChangeRegist;
    @BindView(R.id.login_protocol)
    TextView loginProtocol;


    private Handler mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {

        setListener();

        mHandler = new Handler() {
            @Override
            public void handleMessage(final Message msg) {
                if(msg.what == 1){

                    ToastUtil.show("登录成功！");
                    redirectTo(MainActivity.class);
                    finish();
                }else{
                    ToastUtil.show("帐号或密码错误");
                }
            }
        };
    }

    private void setListener() {
        loginCommitBtn.setOnClickListener(this);
        loginProtocol.setOnClickListener(this);
        loginChangeRegist.setOnClickListener(this);

    }

    private void login(){
        // http
        final Message message = new Message();

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpGo.login(loginUsername.getText().toString(), loginUserpwd.getText().toString(), new HttpListener<LoginResponse>() {
                    @Override
                    public void onSuccess(LoginResponse loginResponse) {

                        if(loginResponse.getCode() == 1){

                            message.what = 1;
                            mHandler.sendMessage(message);

                            String xx = new Gson().toJson(loginResponse.getData());
                            SharedPreferanceUtil.setSp("User",xx);
                            SharedPreferanceUtil.setSp("UserName",loginResponse.getData().getUserName());
                            SharedPreferanceUtil.setSp("UserId",loginResponse.getData().getUserId());
                            SharedPreferanceUtil.setSp("IfHasLogined","1"); // 已登陆标识

                            MyApplication.setUser(loginResponse.getData());

                        }else{

                            message.what = 0;
                            mHandler.sendMessage(message);
                            ToastUtil.show("帐号或密码错误");
                        }

                    }

                    @Override
                    public void onError(String exception) {
                        ToastUtil.show(exception);
                    }
                });
            }
        }).start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_commit_btn: // 登录
                if(judge())
                    login();
                break;
            case R.id.login_protocol: // 协议
                break;
            case R.id.login_change_regist: // 去注册
                redirectTo(RegistorActivity.class);
                setAnimationIn();
                break;
        }
    }

    private boolean judge() {

        // 校验，如果是电话注册后续可自行加入电话正则校验

        if(TextUtils.isEmpty(loginUsername.getText()) || "".equals(loginUsername.getText().toString().trim())){
            ToastUtil.show("用户名不能为空");
            return false;
        }

        if(TextUtils.isEmpty(loginUserpwd.getText()) || "".equals(loginUserpwd.getText().toString().trim())){
            ToastUtil.show("密码不能为空");
            return false;
        }

        return true;
    }

}
